<?php
declare(strict_types=1);

namespace app\admin\business;

use app\common\model\Website as WebsiteModel;

class Website extends AdminBase
{
    private const CONFIG_MERCHANT_USER_SESSION_CODE = 'merchant.session_merchant'; // 商户用户 session key 配置 code
    private $_websiteObj = null;

    public function __construct()
    {
        $this->_websiteObj = new WebsiteModel();
    }

    public function findWebsiteByCode(string $code): array
    {
        $website = null;
        try {
            $website = $this->_websiteObj->findWebsiteByCode($code);
        } catch (\Exception $e) {
            logErr('通过站点标识读取站点信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $code, __METHOD__, $e->getMessage()));
        }

        if (empty($website)) {
            return [];
        }

        return $website->toArray();
    }

    public function checkPermissionToOperateWebsite(array $website): bool
    {
        $merchant = session(config(self::CONFIG_MERCHANT_USER_SESSION_CODE));
        if ($merchant['supper'] != 1 && $website['enterprise_id'] != $merchant['enterprise_id']) {
            return false;
        }

        return true;
    }
}
